
import { result } from 'lodash';
import { async } from '@angular/core/testing';
/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-06-29 15:45:18
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-04 14:32:30
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { MessageService } from '@abp/message/message.service';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { InspectionModelServiceProxy, InspectionSeverityDescDto, InspectionTypeDescDto, InspectionItemInfoDto } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
@Component({
    selector: 'app-business',
    templateUrl: './business.component.html',
    styleUrls: ['./business.component.less']
})
export class BusinessComponent extends AppComponentBase {
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('typePaginator') typePaginator: Paginator;
    @ViewChild('typeData') typeData: Table;
    @ViewChild('sonPaginator') sonPaginator: Paginator;
    @ViewChild('sonData') sonData: Table;
    display: boolean = false;
    parentDia: boolean = false;
    childDia: boolean = false;
    ngDisplayForm = new InspectionSeverityDescDto();
    ngparentForm = new InspectionTypeDescDto();
    ngchildForm = new InspectionItemInfoDto();
    ccRole: any = [];//异常抄送角色
    unitsList: any = [];//角色列表
    deptList: any = [];//部门列表
    organizationUnitsId: any;//新增二级异常时 保存的 organizationUnitsId
    rolesId: any;//新增二级异常时 保存的 rolesId
    pid: string;//父级选中的id
    saving: boolean = false;//新增异常等级保存按钮  检验节流
    savingNgParentDia: boolean = false;//新增一级异常保存按钮  检验节流
    savingNgChildDia: boolean = false;//新增二级异常保存按钮  检验节流
    saveFlag: boolean = true
    selected = [];
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _inspectionModelServiceProxy: InspectionModelServiceProxy,
    ) {
        super(injector);
    }

    ngOnInit() {
        this.getRoleNameList()
        this.getUnitsList()
    }

    //查询 异常严重程度列表
    getSeverityList(event?: LazyLoadEvent): void {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this._inspectionModelServiceProxy.getInspectionSeverityDescList(
            this.primengTableHelper.getSorting(this.dataTable),//undefined
            this.primengTableHelper.getMaxResultCount(this.paginator, event),//10
            this.primengTableHelper.getSkipCount(this.paginator, event)//0
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    //查询 角色列表
    getRoleNameList(): void {
        this._inspectionModelServiceProxy.getRoleManagerNameList()
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.unitsList = result;
            });

    }
    //查询 责任部门
    getUnitsList(): void {
        this._inspectionModelServiceProxy.getOrganizationUnitsList()
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.deptList = result;
            });

    }

    //查询 一级异常列表
    getDescList(event?: LazyLoadEvent): void {
        if (this.primengTypeTableHelper.shouldResetPaging(event)) {
            this.typePaginator.changePage(0);
            return;
        }
        this._inspectionModelServiceProxy.getInspectionTypeDescList(
            this.primengTypeTableHelper.getSorting(this.dataTable),//undefined
            this.primengTypeTableHelper.getMaxResultCount(this.typePaginator, event),//10
            this.primengTypeTableHelper.getSkipCount(this.typePaginator, event)//0
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.primengTypeTableHelper.totalRecordsCount = result.totalCount;
                this.primengTypeTableHelper.records = result.items;
                this.primengTypeTableHelper.hideLoadingIndicator();
                this.pid = this.primengTypeTableHelper.records[0].id
                this.getInfoList(this.pid, event)

            });

    }
    //查询 二级异常列表
    getInfoList(pid: string, event?: LazyLoadEvent): void {
        if (this.primengSonTableHelper.shouldResetPaging(event)) {
            this.sonPaginator.changePage(0);
            return;
        }
        this._inspectionModelServiceProxy.getInspectionItemInfoList(
            pid,
            this.primengSonTableHelper.getSorting(this.dataTable),//undefined
            this.primengSonTableHelper.getMaxResultCount(this.sonPaginator, event),//10
            this.primengSonTableHelper.getSkipCount(this.sonPaginator, event)//0
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.primengSonTableHelper.totalRecordsCount = result.totalCount;
                this.primengSonTableHelper.records = result.items;
                this.primengSonTableHelper.hideLoadingIndicator();
            });

    }
    //新增  异常严重等级
    addSeverityDesc(): void {
        let ccRole = []
        this.ngDisplayForm.ccRole = []
        this.saving = true
        this.ccRole.forEach(element => {
            ccRole.push(Number(element.id))
        });
        this.ngDisplayForm.ccRole = ccRole
        this.ngDisplayForm.isUsed = this.ngDisplayForm.isUsed ? 1 : 0
        if (this.saveFlag) {
            this.saveFlag = false
            this._inspectionModelServiceProxy.createOrUpdateInspectionSeverityDesc(
                this.ngDisplayForm
            )
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe(result => {
                    this.saveFlag = true
                    if (result.code == 0) {
                        this.getSeverityList()
                        this.display = false
                        this._messageService.success('添加成功');
                    } else {
                        this._messageService.error(result.message)
                    }
                });
        }
    }
    //新增弹窗隐藏
    displayclose(): void {
        this.ngDisplayForm = new InspectionSeverityDescDto();
        this.ngparentForm = new InspectionTypeDescDto();
        this.ngchildForm = new InspectionItemInfoDto();
        this.ccRole = []
        this.organizationUnitsId = null
        this.rolesId = null
    }
    //新增 一级异常
    addLevelDesc(): void {
        this.savingNgParentDia = true
        this.ngparentForm.isUsed = this.ngparentForm.isUsed ? 1 : 0
        if (this.saveFlag) {
            this.saveFlag = false
            this._inspectionModelServiceProxy.createOrUpdateInspectionTypeDesc(
                this.ngparentForm
            )
                .pipe(finalize(() => { this.savingNgParentDia = false }))
                .subscribe(result => {
                    this.saveFlag = true
                    if (result.code == 0) {
                        this.getDescList()
                        this.parentDia = false
                        this._messageService.success('添加成功');
                    } else {
                        this._messageService.error(result.message)
                    }
                });
        }
    }
    //新增 二级异常
    addInspectionDesc(): void {
        this.savingNgChildDia = true
        this.ngchildForm.isUsed = this.ngchildForm.isUsed ? 1 : 0
        this.ngchildForm.organizationUnitsId = this.organizationUnitsId.id
        this.ngchildForm.rolesId = this.rolesId.id
        this.ngchildForm.typeId = this.pid
        if (this.saveFlag) {
            this.saveFlag = false
            this._inspectionModelServiceProxy.createOrUpdateCreateInspectionItemInfo(
                this.ngchildForm
            )
                .pipe(finalize(() => { this.savingNgChildDia = false }))
                .subscribe(result => {
                    this.saveFlag = true
                    if (result.code == 0) {
                        this.getInfoList(this.pid)
                        this.childDia = false
                        this._messageService.success('添加成功');
                    } else {
                        this._messageService.error(result.message)
                    }
                });
        }
    }
    //点击父级更换子级列表数据
    changeChild(index): void {
        this.pid = this.typeData.value[index].id
        this.getInfoList(this.pid)
    }
    //修改 异常严重等级 数据
    updateDataTable(index): void {
        this.ngDisplayForm = this.dataTable.value[index]
        this.display = true
        let itemCcRole = this.dataTable.value[index].ccRole.split(",")
        this.ccRole = []

        itemCcRole.forEach(cI => {
            this.unitsList.forEach(uI => {
                if (cI == uI.displayName) {
                    this.ccRole.push(uI)
                }

            });
        });


    }
    //修改 一级异常 数据
    updateLevelTable(index): void {
        this.ngparentForm = this.typeData.value[index]
        this.parentDia = true
    }
    //修改 二级异常 数据
    updateChildTable(index): void {
        this.ngchildForm = this.sonData.value[index]
        this.childDia = true
        this.organizationUnitsId = null
        this.deptList.forEach(uI => {
            if (this.sonData.value[index].organizationUnitsId == uI.id) {
                this.organizationUnitsId = uI
            }

        });
        this.unitsList.forEach(uI => {
            if (this.sonData.value[index].rolesId == uI.id) {
                this.rolesId = uI
            }
        });
    }
    // 删除事件
    deleteEv(id, type): void {
        if (this.saveFlag) {
            this.saveFlag = false
            this._messageService.confirm(
                '是否删除该条数据？',
                (isConfirmed) => {
                    if (isConfirmed) {
                        // deleteInspectionSeverityDesc 异常严重
                        // deleteInspectionTypeDesc 一级
                        //deleteInspectionItemInfo 二级
                        if (type == 'deleteInspectionSeverityDesc') {
                            this._inspectionModelServiceProxy.deleteInspectionSeverityDesc(
                                id
                            )
                                .pipe(finalize(() => { }))
                                .subscribe(result => {
                                    this.saveFlag = true
                                    if (result.code == 0) {
                                        this.getSeverityList()
                                        this._messageService.success('删除成功');
                                    } else {
                                        this._messageService.error(result.message)
                                    }

                                });
                        } else if (type == 'deleteInspectionTypeDesc') {
                            this._inspectionModelServiceProxy.deleteInspectionTypeDesc(
                                id
                            )
                                .pipe(finalize(() => { }))
                                .subscribe(result => {
                                    this.saveFlag = true
                                    if (result.code == 0) {
                                        this.getDescList()
                                        this._messageService.success('删除成功');
                                    } else {
                                        this._messageService.error(result.message)
                                    }
                                });
                        } else if (type == 'deleteInspectionItemInfo') {
                            this._inspectionModelServiceProxy.deleteInspectionItemInfo(
                                id
                            )
                                .pipe(finalize(() => { }))
                                .subscribe(result => {
                                    this.saveFlag = true
                                    if (result.code == 0) {
                                        this.getInfoList(this.pid)
                                        this._messageService.success('删除成功');
                                    } else {
                                        this._messageService.error(result.message)
                                    }
                                });
                        }
                    }
                }
            );
        }

    }



}
